// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var top = require("../top/top_main");
	var footer = require("../footer/footer_main");
	var databind = require("databind");
	var chart;
	var time;
	var specification;
	var bulkstockName;
	var chartDates = [];
	var chartPrices = [];
	var bulkstockCategoryId;

	var init = function() {
		specification = decodeURI(base.getUrlParam("specification"));
		bulkstockName = decodeURI(base.getUrlParam("bulkstockName"));
		bulkstockCategoryId = base.getUrlParam("bulkstockCategoryId");
		inquireBulkstockCategoryDetail(bulkstockCategoryId);
		inquireBulkstockGatherList();
		top.init();
		footer.init();
		router.init();
		$("#head").removeClass("none");
		$("#content").removeClass("none");
		$("#footer").removeClass("none");
		$("#publicInquire").addClass("nav_active");
		initEvent();
	};

	var initEvent = function() {
		databind.fillInput1($("#content"), {
			action: "inquireBulkstockGatherLatestList.json?bulkstockName=" + bulkstockName + "&specification=" + specification,
			version: "v1.0",
			nocode: true,
		}, function() {
			var dateTime = $("#issueDateTime").val().replace(/[^0-9]/ig,""); 
			var d = dateTime.substring(0,4) + ',' + dateTime.substring(4, 6) + ',' + dateTime.substring(6, 8) + ',' +dateTime.substring(8, 10) + ',' +dateTime.substring(10, 12) + ',' +dateTime.substring(12, 14);
			calTime(d);
			$("#monitorDataTime").html(time);

			if ($(".riseFall").html()*1 > 0) {
				$(".priceInfo1").addClass("rise");
				$(".priceInfo1").removeClass("fall");
				$(".upAndDown").html("+");
			} else if($(".riseFall").html()*1 < 0) {
				$(".priceInfo1").addClass("fall");
				$(".priceInfo1").removeClass("rise");
				$(".upAndDown").html("");
			}
		});

	};

	//融资所需材料
	var inquireBulkstockCategoryDetail = function(bulkstockCategoryId) {
		base.fnAjax({
			action: "inquireBulkstockCategoryDetail.json",
			async: true,
			nocode: true,
			oData: {
				bulkstockCategoryId: bulkstockCategoryId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var str = "";
					for (var j = 0; j < data.datas[0].templates.length; j++) {
						if (j == 0) {
							str = "该类货物融资时需要：";
						}
						str += data.datas[0].templates[j].templateName + ",";
					}
					str = str.substring(0, str.length - 1);
					$("#bulkstockMaterial").html(str);
				} else {
					//                  base.alert(2,"信息获取失败");
				}
			}
		});
	};

	//折线图
	var showChart = function() {
		chart = new Highcharts.Chart({
			//HighCharts中chart属性配置  
			chart: {
				renderTo: 'container', //div 标签  
				type: 'line', //图表类型  

				/******************以下chart配置可选******************/
				backgroundColor: "#FFF", //图表背景色  
				borderWidth: 0, //图表边框宽度  
				borderRadius: 0, //图表边框圆角角度  
				plotBackgroundColor: "#FFF", //主图表区背景颜色  
				plotBorderColor: '#FFF', //主图表边框颜色  
				plotBorderWidth: 0, //主图表边框宽度  
				shadow: false, //是否设置阴影  
				zoomType: 'xy', //拖动鼠标放大图表的方向
				//				width: "auto",
				height: "300"
			},
			title: {
				text: ''
			},
			credits: {
				//enable:true,默认就为true，可以不配置  
				//如果想要去除版权（也就是不显示），只需要设置enable:false即可  
				href: 'http://www.52wulian.org', //链接地址  
				position: { //文字的位置  
					x: -30,
					y: -30
				},
				style: { //文字的样式  
					color: 'red',
					fontWeight: 'bold'
				},
				enabled: false, //不显示highCharts版权信息,不显示为false  
				text: '' //文字  
			},
			/******************  
			**Colors-颜色属性为可选配置  
			**通过配置配置colors，可以轻松的设置数据列的颜色  
			1、颜色代码可以是十六进制，也可以是英文单词，  
			  还可以是RGB，如同css  
			  2、默认是从第一个数据列起调用第一个颜色代码，  
			  有多少个数据列调用相应数量的颜色  
			  3、当数据列大于默认颜色数量时，重复从第一个  
			  颜色看是调用  
			******************/
			colors: [
				'#FF855D',
				'#000000', //黑  
				'#FF0000', //红  
				'#00FF00', //绿  
				'#0000FF', //蓝  
				'#FFFF00', //黄  
				'#FF00FF', //紫  
				'#FFFFFF', //紫  
			],
			exporting: {
				//enabled:true,默认为可用，当设置为false时，图表的打印及导出功能失效  
				buttons: { //配置按钮选项  
					printButton: { //配置打印按钮  
						width: 50,
						symbolSize: 20,
						borderWidth: 2,
						borderRadius: 0,
						hoverBorderColor: 'red',
						height: 30,
						symbolX: 25,
						symbolY: 15,
						x: -200,
						y: 20
					},
					exportButton: { //配置导出按钮  
						width: 50,
						symbolSize: 20,
						borderWidth: 2,
						borderRadius: 0,
						hoverBorderColor: 'red',
						height: 30,
						symbolX: 25,
						symbolY: 15,
						x: -150,
						y: 20
					}
				},
				filename: '52wulian.org', //导出的文件名  
				type: 'image/png', //导出的文件类型  
				width: 800 //导出的文件宽度  
			},
			labels: {
				items: [{
					//标签代码（html代码）  
					html: '',
					style: { //设置标签位置  
						left: '980px',
						top: '-20px'
					}
				}],
				style: { //设置标签颜色  
					color: 'rgb(0,0,255)'
				}
			},
			xAxis: {
				categories: chartDates.reverse(),
				lineColor: "#E3E2E2"
			},
			series: [{
				name: bulkstockName + "(" + specification + ")",
				data: chartPrices.reverse()
			}]
		});
	};

	//计算当前时间到目前时间数
	var calTime = function(dateTime) {
		var year, month, day, hour, minute, second;
		var da = dateTime.split(',');
		var nowDate = new Date();
		year = nowDate.getFullYear() - da[0];
		month = nowDate.getMonth() - da[1] + 1;
		day = nowDate.getDate() - da[2];
		hour = nowDate.getHours() - da[3];
		minute = nowDate.getMinutes() - da[4];
		second = nowDate.getSeconds() - da[5];
		if (second < 0) {
			second += 60;
			minute -= 1;
		}
		if (minute < 0) {
			minute += 60;
			hour -= 1;
		}
		if (hour < 0) {
			hour += 24;
			day -= 1;
		}
		if (day < 0) {
			if (da[1] == 1 || da[1] == 3 || da[1] == 5 || da[1] == 7 || da[1] == 8 || da[1] == 10 || da[1] == 12) {
				day += 30;
				month -= 1;
			} else if (da[1] == 2) {
				if (da[0] % 100 == 0 && da[0] % 4 == 0) {
					day += 29;
					month -= 1;
				} else {
					day += 28;
					month -= 1;
				}
			} else {
				day += 31;
				month -= 1;
			}
		}
		if (month < 0) {
			month += 12;
			year -= 1;
		}
		if (year < 0) {
			//			alert("日期出错！");
		}

		if (year == 0) {
			if (month == 0) {
				if (day == 0) {
					if (hour == 0) {
						if (minute == 0) {
							if (second == 0) {
								time = "0&nbsp;秒前";
							} else {
								time = second + "&nbsp;秒前";
							}
						} else {
							time = minute + "&nbsp;分钟&nbsp;" + second + "&nbsp;秒前";
						}
					} else {
						time = hour + "&nbsp;小时&nbsp;" + minute + "&nbsp;分钟&nbsp;" + second + "&nbsp;秒前";
					}
				} else {
					time = day + "&nbsp;天&nbsp;" + hour + "&nbsp;小时&nbsp;" + minute + "&nbsp;分钟&nbsp;" + second + "&nbsp;秒前";
				}
			} else {
				time = month + "&nbsp;个月&nbsp;" + day + "&nbsp;天&nbsp;" + hour + "&nbsp;小时&nbsp;" + minute + "&nbsp;分钟&nbsp;" + second + "&nbsp;秒前";
			}
		} else {
			time = year + "&nbsp;年&nbsp;" + month + "&nbsp;个月&nbsp;" + day + "&nbsp;天&nbsp;" + hour + "&nbsp;小时&nbsp;" + minute + "&nbsp;分钟&nbsp;" + second + "&nbsp;秒前";
		}
	};

	//查询商品价格汇总
	var inquireBulkstockGatherList = function() {
		base.fnAjax({
			action: "inquireBulkstockGatherList.json",
			oData: {
				auditState: 1,
				pageSize: 180,
				bulkstockName: bulkstockName,
				specification: specification
			}, //传递参数
			nocode: true,
			fnSuccess: function(data) {
				if (data.status == 200) {
					for (var j = 0; j < data.datas.length; j++) {
						chartDates.push(data.datas[j].priceDate.substring(5).replace("-", "."));
						chartPrices.push(parseFloat(data.datas[j].price));
					}
					showChart();
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};

	return {
		init: init
	};
});